﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.Module;
using Frame.Lib.Core.Role;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.User;
using Frame.Lib.Common;

public partial class Admin_Role_SaveModule : PageSelectTree
{
    private ModuleBO Module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }

    private IModuleAccess _access;
    private IModuleAccess Access
    {
        get
        {
            if (_access == null)
            {
                _access = new RoleBO();
            }
            return _access;
        }
    }

    private int RoleID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["RoleID"]));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            BindTree();
            if (RoleID < 0)
            {
                lblName.Text = ((InternalRole)RoleID).ToString();
            }
            else
            {
                lblName.Text = (new RoleBO()).GetName(RoleID);
            }
        }
        tvModule.Attributes.Add("onclick", "CascadSelect('" + tvModule.ClientID + "')");
    }

    private void BindTree()
    {
        IEnumerable<ModuleInfo> list = Module.GetList();

        UserBO user = new UserBO();
        RoleBO role = new RoleBO();

        if (AppSession.UserID != Constants.ADMIN_ID)
        {
            IEnumerable<int> moduleIDs = user.GetModuleIDs(AppSession.UserID, role);
            list = list.Where(m => moduleIDs.Contains(m.ID));
        }

        list = list.Where(T=>T.State == (int)ModuleState.Enable);

        List<int> lstModuleIDs = Access.GetModuleIDs(RoleID);
        Module.BindTreeView(tvModule, list, true,lstModuleIDs);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        List<int> lstModuleIDs = StrUtil.SplitToInt(CheckedIDs);

        Access.SaveModuleIDs(RoleID, lstModuleIDs);
        string strScript = "window.close();returnValue=true";
        JSUtil.ResponseJS(strScript);
    }   
}
